from utils.font_size import FontSize
from components.text import *
from components.rect import *
from settings import *


class ExitComponent:

    def __init__(self, theme, left, seq, sub) -> None:
        self.color = theme
        self.seq = seq
        self.sub = sub
        self.p_height = 50 * SCALE
        self.left = left
        self.top = '左'
        self.name = '出口'
        self.name_size = 20 * SCALE
        self.seq_size = 40 * SCALE
        self.sub_size = int(self.seq_size * 0.6)
        self.name_width = FontSize.get_font_width(self.name, FONT_PATH_A, self.name_size)
        self.inset = 6 * SCALE
        self.seq_width = FontSize.get_font_width(self.seq, FONT_PATH_B, self.seq_size)
        self.sub_width = FontSize.get_font_width(self.sub, FONT_PATH_B, self.sub_size)
        self.rx = max(40 * SCALE, (self.seq_width + self.sub_width) / 2 + self.inset * 2)
        self.ry = (self.p_height - 2 * self.inset) / 2

    def width(self):
        return self.name_width + 2 * self.rx + 3 * self.inset
    
    def height(self):
        return self.p_height
    
    def draw(self, container, x, y):
        rect = DoubleStrokeRectComponent(self.width(), self.p_height, self.color, COLOR_WHITE, 2 * SCALE, 8 * SCALE)
        rect.draw(container, x, y)

        name_y = y + (self.p_height - 2 * self.name_size) / 2 + self.name_size if self.left else y + (self.p_height - self.name_size) / 2
        name = TextAComponent(self.name, COLOR_WHITE, self.name_size)
        name.draw(container, x + self.inset, name_y)

        if self.left:
            top = TextAComponent(self.top, COLOR_WHITE, self.name_size)
            top.draw(container, x + self.inset + (name.width() - top.width()) / 2, y + (self.p_height - 2 * self.name_size) / 2)

        container.add(container.ellipse(center=(x + self.inset * 2 + self.rx + self.name_width, y + self.p_height / 2), r=(self.rx, self.ry), fill=COLOR_WHITE))

        seq = TextBComponent(self.seq, self.color, self.seq_size)
        seq.draw(container, x + 2 * self.inset + self.name_width + (self.rx * 2 - self.seq_width - self.sub_width) / 2, y + (self.p_height - self.seq_size) / 2)

        if len(self.sub):
            sub = TextBComponent(self.sub, self.color, self.sub_size)
            sub.draw(container, x + 2 * self.inset + self.name_width + (self.rx * 2 - self.seq_width - self.sub_width) / 2 + self.seq_width, y + (self.p_height - self.sub_size * 0.6) / 2)